//
//  BaseTableViewVC.m
//  GasCome
//
//  Created by zfc on 2020/1/7.
//  Copyright © 2020 zfc. All rights reserved.
//

#import "BaseTableViewVC.h"

@interface BaseTableViewVC () 
@property (nonatomic, strong) NSMutableDictionary *cellHeightsDictionary;  //存储cell高度 防止self.sizing引起刷新抖动

@end

@implementation BaseTableViewVC

- (void)viewDidLoad {
    [super viewDidLoad];

    self.page = 1;
    self.cellHeightsDictionary = @{}.mutableCopy;

    if (self.estimatedRowHeight != 0) {
        self.tableView.rowHeight = UITableViewAutomaticDimension;
        self.tableView.estimatedRowHeight = self.estimatedRowHeight;
    }

    [self fetchData];
    [self.view addSubview:self.tableView];
    [self addRefresh];

}

- (void) fetchData {
    [self endRefresh];
}

- (void) fetchDataWithPage:(NSInteger) page {
    [self endRefresh];
}

- (void) addRefresh {
    
    WeakSelf;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        StrongSelf;
        strongSelf.page = 1;
        [strongSelf.tableView.mj_footer resetNoMoreData];
        [strongSelf fetchData];
    }];

    if (self.isAddFooter) {
        self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            StrongSelf;
            strongSelf.page = strongSelf.page + 1;
            [strongSelf fetchData];
        }];
        self.tableView.mj_footer.automaticallyHidden = YES;
    }
}

- (void)setIsAddFooter:(BOOL)isAddFooter {
    if (isAddFooter) {
        WeakSelf;
        self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            StrongSelf;
            strongSelf.page = strongSelf.page + 1;
            [strongSelf fetchData];
        }];
        self.tableView.mj_footer.automaticallyHidden = YES;
    }
}

- (void)setIsRemoveHeader:(BOOL)isRemoveHeader {
    if (isRemoveHeader) {
        self.tableView.mj_header = nil;
    }
}

- (void) refresh {
    [self.tableView.mj_header beginRefreshing];
}

- (void) endRefresh {
    [self.tableView.mj_header endRefreshing];
    [self.tableView.mj_footer endRefreshing];
}

#pragma mark - lazy
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH - kTopHeight - kBottomWhiteHeight) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
    }
    return _tableView;
}

- (NSMutableArray *)dataList {
    if (!_dataList) {
        _dataList = [NSMutableArray array];
    }
    return _dataList;
}


#pragma mark - UITableViewDelegate & UITableViewDataSource
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 0;
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 0.0;
}

- (CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.001;
}

- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.001;
}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BaseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ID"];
    if (!cell) {
        cell = [[BaseTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ID"];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.cellHeightsDictionary setObject:@(cell.frame.size.height) forKey:indexPath];
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSNumber *height = [self.cellHeightsDictionary objectForKey:indexPath];
    if (height.doubleValue) return height.doubleValue;
    return UITableViewAutomaticDimension;
}


@end
